package org.tri.form;

import java.io.*;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.tri.entity.AbstractAnimal;
import org.tri.entity.Venta;

/**
 * La clase {@code TRIHelper}
 * @author kiira
 * @version 1.0, Mar 23, 2012
 */
public class TRIHelper {
    public static TRI guardar( Venta venta ) throws FileNotFoundException,
                                                        IOException,
                                                        InvalidFormatException {
        TRI tri = new TRI( venta );
        
        //creamos el file chooser
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle( "Seleccione la ubicación y nombre del TRI "
                                + "a crear" );
        
        boolean res = chooser.showSaveDialog( null ) 
                      == JFileChooser.APPROVE_OPTION;
        
        if ( res ) {
            File salidaTRI;

            if ( chooser.getSelectedFile().getAbsolutePath().endsWith( ".xls" ) )
                salidaTRI = chooser.getSelectedFile();
            else
                salidaTRI = new File(
                        chooser.getSelectedFile().getAbsolutePath() + ".xls" );
            
            File salidaTxt = new File( salidaTRI.getAbsolutePath().replace(
                    ".xls", ".txt" ) );
            
            tri.guardar( salidaTRI );
            
            guardarTxt( venta, salidaTxt );
            
        }//end if        
        
        return res ? tri : null;
        
    }//end método guardar
    
    public static void imprimir( Venta venta ) throws FileNotFoundException, 
                                                      IOException, 
                                                      InvalidFormatException {
        TRI tri = new TRI( venta );
        
        tri.abrirEImprimir();
        
    }//end método imprimir   
    
    private static void guardarTxt( Venta venta, File salidaTxt ) {
        BufferedWriter writer = null;
        
        try {
            writer = new BufferedWriter( new FileWriter( salidaTxt ) );
            
            List<AbstractAnimal> animales = venta.obtenerAnimales();
            
            for ( AbstractAnimal animal : animales ) {
                writer.write( animal.getCaravana() );
                writer.newLine();
                
            }//end for

        } catch ( FileNotFoundException fnfEx ) {
            
        } catch ( UnsupportedEncodingException ueEx ) {
                        
        } catch ( IOException ioEx ) {
            
        } finally {
            try {
                if ( writer != null )
                    writer.close();
                
            } catch ( IOException ioEx ) {
                                
            }
            
        }
        
    }//end método guardarTxt
       
    public static void guardarEImprimir( Venta venta ) {
        TRI tri = null;
        
        try {
            tri = guardar( venta );

        } catch ( IOException iOex ) {
            JOptionPane.showMessageDialog( null,
                                           "No se pudo generar el TRI!",
                                           "Error",
                                           JOptionPane.ERROR_MESSAGE );

        } catch ( Exception ex ) {
            //ignore            
        }

        if ( tri != null ) 
            try {
                tri.abrirEImprimir();
                
            }catch ( Exception ex ) {
            JOptionPane.showMessageDialog( null,
                                           "El programa no pudo abrir el "
                                           + "archivo para imprimir.\n "
                                           + "Deberá abrirlo manualmente.",
                                           "Información",
                                           JOptionPane.INFORMATION_MESSAGE );

            }         
                
    }//end método guardarEImprimir
            
}//end clase TRIHelper
